package com.atguigu.edu.realtime.app.dwd.log;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.util.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.OutputTag;

import java.util.List;
import java.util.Map;


//用户跳出明细事务事实表
public class DwdTrafficUserJumpDetail {
    public static void main(String[] args) throws Exception{
        //TODO 1,基本环境准备
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        //TODO 2,检查点相关设置
       /*
        //开启检查点
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE)
        //设置检查点超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60000L);
        //设置job取消以后检查点是否保留
        env.getCheckpointConfig().enableExternalizedCheckpoints(
                CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION
        );
        //设置两个检查点之间最小时间间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000L);
        //设置重启策略
        env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3,3000L));
        //设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        //操作Hadoop的用户
        System.setProperty("HADOOOP_USER_NAME","atguigu");
        */
        //TODO 3,从Kafka的page_log主题中读取数据，封装为流
        String topic="dwd_traffic_page_log";
        String groupId = "dwd_traffic_user_jump_detail";
        FlinkKafkaConsumer<String> kafkaConsumer = MyKafkaUtil.getKafkaConsumer(topic, groupId);
        DataStreamSource<String> kafkaStrDS = env.addSource(kafkaConsumer);
        //TODO 4,对流中的数据进行类型转换 jsonStr->jsonObj
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaStrDS.map(JSON::parseObject);
        //TODO 5,指定watermark以及提取时间字段
        SingleOutputStreamOperator<JSONObject> withWatermarkDS = jsonObjDS.assignTimestampsAndWatermarks(
                WatermarkStrategy.<JSONObject>forMonotonousTimestamps()
                        // WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<JSONObject>() {
                                    @Override
                                    public long extractTimestamp(JSONObject jsonObj, long recordTimestamp) {
                                        return jsonObj.getLong("ts");
                                    }
                                }
                        )
        );
        //TODO 6,按照mid分组
        KeyedStream<JSONObject, String> keyedDS = withWatermarkDS.keyBy(jsonObject -> jsonObject.getJSONObject("common").getString("mid"));
        //TODO 7,使用flinkCEP判断是否为跳出行为
        //定义pattern
        Pattern<JSONObject,JSONObject> pattern = Pattern.<JSONObject>begin("first").where(
                new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObj) {
                        String lastPageId = null;
                        try {
                            lastPageId = jsonObj.getJSONObject("page").getString("last_page_id");
                        } catch (Exception e) {
//                            throw new RuntimeException(e);
                            return false;
                        }
                        return StringUtils.isEmpty(lastPageId);
                    }
                }
        ).next("second").where(
                new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObj) {
                        String lastPageId = null;
                        try {
                            lastPageId = jsonObj.getJSONObject("page").getString("last_page_id");
                        } catch (Exception e) {
//                            throw new RuntimeException(e);
                            return false;
                        }
                        return StringUtils.isEmpty(lastPageId);
                    }
                }
        ).within(Time.seconds(10));
        //将pattern应用到流上
        PatternStream<JSONObject> patternDS = CEP.pattern(keyedDS, pattern);
        //从流中提取数据,写入侧输出流
        OutputTag<String> timeOutTag = new OutputTag<String>("timeOutTag") {
        };
        SingleOutputStreamOperator<String> mathDS = patternDS.select(
                timeOutTag,
                new PatternTimeoutFunction<JSONObject, String>() {
                    @Override
                    public String timeout(Map<String, List<JSONObject>> pattern, long timeoutTimestamp) throws Exception {
                        return pattern.get("first").get(0).toJSONString();
                    }
                },
                new PatternSelectFunction<JSONObject, String>() {
                    @Override
                    public String select(Map<String, List<JSONObject>> pattern) throws Exception {
                        return pattern.get("first").get(0).toJSONString();
                    }
                }
        );
        //TODO 8,将完全匹配的数据和超时数据进行合并
        DataStream<String> ujdDS = mathDS.union(mathDS.getSideOutput(timeOutTag));
        //TODO 9,将跳出行为发送到Kafka主题中
        ujdDS.print(">>>>");
        ujdDS.addSink(MyKafkaUtil.getKafkaProducer("dwd_traffic_user_jump_detail"));

        env.execute();
    }
}

